﻿//Mirco Töpfer

#region Usings

using System;
using System.Collections.Generic;
using System.ComponentModel;
using pheidippides.common.Classes;
using pheidippides.dataaccess.Classes;
using pheidippides.logging.Classes;

#endregion

namespace pheidippides.business.Classes
{
    /// <summary>
    /// The Veranstaltung
    /// </summary>
    public class Veranstaltung : INotifyPropertyChanged
    {
        #region Private Members

        private readonly VeranstaltungData _veranstaltungData;

        #endregion

        #region Constructor

        /// <summary>
        /// Prevents a default instance of the <see cref="Veranstaltung"/> class from being created.
        /// </summary>
        private Veranstaltung()
        {
            Logger.GetInstance().LogMessage("Generierung neuer Veranstaltung", LogLevel.Debug);

            _veranstaltungData = new VeranstaltungData();
            _veranstaltungData.Id = Guid.NewGuid();
            _veranstaltungData.Datum = null;
            _veranstaltungData.Name = "";
            _veranstaltungData.Notiz = "";
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="Veranstaltung"/> class from being created.
        /// </summary>
        /// <param name="data">The data.</param>
        private Veranstaltung(VeranstaltungData data)
        {
            _veranstaltungData = data;
        }

        #endregion

        #region Factory Classes

        /// <summary>
        /// Gets the new.
        /// </summary>
        /// <returns></returns>
        public static Veranstaltung GetNew()
        {
            return new Veranstaltung();
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns></returns>
        public static List<Veranstaltung> GetAll()
        {
            return GetVeranstaltungFromVeranstaltungData(VeranstaltungDataAccess.GetAll());
        }

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static Veranstaltung GetById(Guid id)
        {
            VeranstaltungData data = VeranstaltungDataAccess.Get(id);
            
            if (data == null) 
                return null;
            
            return new Veranstaltung(data);
        }

        #endregion

        #region Public Accessors

        /// <summary>
        /// Gets the id.
        /// </summary>
        public Guid Id
        {
            get { return _veranstaltungData.Id; }
        }

        /// <summary>
        /// Gets or sets the datum.
        /// </summary>
        /// <value>
        /// The datum.
        /// </value>
        public DateTime? Datum
        {
            get { return _veranstaltungData.Datum; }
            set { _veranstaltungData.Datum = value; }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public String Name
        {
            get { return _veranstaltungData.Name; }
            set { _veranstaltungData.Name = value; }
        }

        /// <summary>
        /// Gets or sets the notiz.
        /// </summary>
        /// <value>
        /// The notiz.
        /// </value>
        public String Notiz
        {
            get { return _veranstaltungData.Notiz; }
            set { _veranstaltungData.Notiz = value; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance.
        /// </summary>
        public void Save()
        {
            VeranstaltungDataAccess.Save(_veranstaltungData);
        }

        /// <summary>
        /// Deletes this instance.
        /// </summary>
        public void Delete()
        {
            VeranstaltungDataAccess.Delete(_veranstaltungData);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets the veranstaltung from veranstaltung data.
        /// </summary>
        /// <param name="veranstaltungData">The veranstaltung data.</param>
        /// <returns></returns>
        private static List<Veranstaltung> GetVeranstaltungFromVeranstaltungData(
            IEnumerable<VeranstaltungData> veranstaltungData)
        {
            List<Veranstaltung> veranstaltung = new List<Veranstaltung>();

            foreach (VeranstaltungData data in veranstaltungData)
            {
                veranstaltung.Add(new Veranstaltung(data));
            }
            return veranstaltung;
        }

        #endregion

        #region INotifyPropertyChanged Member

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}